﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        Socket socketSend;
        private void btnStart_Click(object sender, EventArgs e)
        {
            //创建负责通讯的Socket
            socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(txtServer.Text);
            IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text));
            //获得要链接的服务器的应用程序的ip地址和端口号
            socketSend.Connect(point);
            ShowMsg("连接成功");
            Thread th = new Thread(Receive);
            th.Start();

        }

        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="str"></param>
        void ShowMsg(string str)
        {
            txtLog.AppendText(str + "\r\n");
        }

        /// <summary>
        /// 客户端给服务器发送信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            string str = txtMsg.Text.Trim();
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
            socketSend.Send(buffer);
        }

        /// <summary>
        /// 客户端接收服务器端发来的消息
        /// </summary>
        void Receive()
        {
            while (true)
            {
                try
                {
                    //接收服务器发来的消息
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    //实际接收到的有效字节
                    int r = socketSend.Receive(buffer);
                    if (r == 0)
                    {
                        break;
                    }
                    if (buffer[0]==0)//表示接收到的是文字消息
                    {
                        string str = Encoding.UTF8.GetString(buffer, 1, r-1);
                        ShowMsg(socketSend.RemoteEndPoint + ":" + str);
                    }
                    else if (buffer[0]==1)//表示接受到的是文件
                    {
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.InitialDirectory = @"C:\";
                        sfd.Title = "请选择要保存的文件";
                        sfd.Filter = "所有文件|*.*";
                        sfd.ShowDialog(this);
                        string path = sfd.FileName;
                        using (FileStream fsWrite=new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
                        {
                            fsWrite.Write(buffer,1,r-1);
                        }
                        MessageBox.Show("保存成功");
                    }
                    else if (buffer[0]==2)//表示接收到的是震动
                    {
                        ZD();
                    }      
                }
                catch(Exception e)
                {
                }

            }
        }

        /// <summary>
        /// 震动
        /// </summary>
        void ZD()
        {
            for (int i = 0; i < 1000; i++)
            {
                this.Location = new Point(200,200);
                this.Location = new Point(280,200);
            }
        }
    }
}
